package com.hkbigdata.watermark;

import com.hkbigdata.entry.WaterSensor;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.EventTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.time.Duration;

/**
 * @author liuanbo
 * @creat 2023-04-24-15:22
 * @see 2194550857@qq.com
 *
 * ws_1001,1577844001L,46
 * ws_1001,1577844001,45
 * ws_1001,1577844003,46
 * ws_1001,1577844004,50
 * ws_1001,1577844005,20
 * ws_1001,1577844006,30
 * ws_1001,1577844007,100
 * ws_1001,1577844008,200
 * ws_1001,15778440010,100
 * ws_1001,15778440012,90
 */
public class Flink01_Window_EventTimeTumbling {
    public static void main(String[] args) throws Exception {
        //1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> waterDS = env.socketTextStream("hadoop102", 9999).
                map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0],
                                Long.parseLong(split[1]),
                                Integer.parseInt(split[2])
                        );
                    }
                });
        //1.1watermark生成策略，自增
/*        WatermarkStrategy<WaterSensor> waterSensorWatermarkStrategy = WatermarkStrategy.<WaterSensor>forMonotonousTimestamps().withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
            @Override
            public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                return element.getTs() * 1000;
            }
        });*/

        //1.2watermark生成策略，允许乱序数据
        WatermarkStrategy<WaterSensor> waterSensorWatermarkStrategy1 = WatermarkStrategy.<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(2)).withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
            @Override
            public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                return element.getTs() * 1000L;
            }
        });


        //2.分配策略
        SingleOutputStreamOperator<WaterSensor> waterSensorSingleOutputStreamOperator = waterDS.assignTimestampsAndWatermarks(waterSensorWatermarkStrategy1);

        //3.分组
        KeyedStream<WaterSensor, String> waterSensorStringKeyedStream = waterSensorSingleOutputStreamOperator.keyBy(WaterSensor::getId);
        //4.开窗
        //4.1滚动窗口
        WindowedStream<WaterSensor, String, TimeWindow> window = waterSensorStringKeyedStream.window(TumblingEventTimeWindows.of(Time.seconds(5)));
        //4.2滑动窗口
        WindowedStream<WaterSensor, String, TimeWindow> window1 = waterSensorStringKeyedStream.window(SlidingEventTimeWindows.of(Time.seconds(10), Time.seconds(5)));
        //4.3会话窗口
        WindowedStream<WaterSensor, String, TimeWindow> window2 = waterSensorStringKeyedStream.window(EventTimeSessionWindows.withGap(Time.seconds(5)));

        //5.聚合
        window.sum("vs").print();

        env.execute();
    }
}
